package com.zadtek.shopmall.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.LoginResponse;
import com.zadtek.shopmall.bean.OperateResponseBean2;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.MD5;
import com.zadtek.shopmall.utils.SPUtil;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ResetPasswordActivity extends AbsActivity {
    private EditText etOldPassword,etNewPassword,etNewPasswordAgain;
    private TextView tvConfirmEdit,tvTopTitle;
    private ImageView ivBack;
    @Override
    public int setView() {
        return R.layout.activity_reset_password;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.title);
        tvTopTitle.setText("修改密码");
        tvConfirmEdit = (TextView) findViewById(R.id.tv_confirm_edit);

        etNewPassword = (EditText)findViewById(R.id.et_new_password);
        etNewPasswordAgain = (EditText)findViewById(R.id.et_new_password_again);
        etOldPassword = (EditText)findViewById(R.id.et_old_password);
    }

    private void  initLogic(){
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        tvConfirmEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitChangePassword();
            }
        });
    }

    private void submitChangePassword() {
        String old = etOldPassword.getText().toString();
        String newPassword = etNewPassword.getText().toString();
        String newPasswordAgain = etNewPasswordAgain.getText().toString();
        if (TextUtils.isEmpty(old)||TextUtils.isEmpty(newPassword)||TextUtils.isEmpty(newPasswordAgain)) {
            ToastUtil.showToast("请填写完所有信息，再提交！");
            return;
        }
        if(!TextUtils.equals(newPassword,newPasswordAgain)){
            ToastUtil.showToast("两次输入的新密码不一致！");
            return;
        }
        Map<String, Object> params = NetTool.newParams();
        params.put("oldpassword", MD5.getStringMD5(old));
        params.put("newpassword",MD5.getStringMD5(newPassword));
        params.put("repeatpassword",MD5.getStringMD5(newPasswordAgain));
        NetTool.getApi().changePassword(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResponse<OperateResponseBean2>>() {
                    @Override
                    public void accept(BaseResponse<OperateResponseBean2> baseResponse) throws Exception {
                        if(baseResponse.getCode() == 0 && baseResponse.getData().isSuccess()){
                            ToastUtil.showToast("重置密码成功！");
                            finish();
                        }else{
                            ToastUtil.showToast("重置密码失败！"+baseResponse.getData().getMessage());
                        }
                    }
                });
    }

}
